package com.gxks.lhs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxks.lhs.entity.Ad;
import com.gxks.lhs.mapper.AdMapper;
import com.gxks.lhs.service.IAdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 主页轮播图 服务实现类
 * </p>
 *
 * @author LuoHaisong
 * @since 2024-03-29
 */
@Service
public class AdServiceImpl extends ServiceImpl<AdMapper, Ad> implements IAdService {
    @Override
    public boolean saveAd(Ad ad) {
        return saveOrUpdate(ad);
    }

    @Override
    public boolean deleteAd(Long adId) {
        return removeById(adId);
    }

    @Override
    public Ad queryAdById(Long adId) {
        return getById(adId);
    }

    @Override
    public Page<Ad> queryAllAdForUser() {
        LambdaQueryWrapper<Ad> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Ad::getStatus, true);
        return page(new Page<>(1, 20), lambdaQueryWrapper);
    }

    @Override
    public Page<Ad> queryAllAdForAdmin(Integer current, Integer size) {
        LambdaQueryWrapper<Ad> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return page(new Page<>(current, size), lambdaQueryWrapper);
    }

    @Override
    public boolean offAd(Long adId) {
        LambdaUpdateWrapper<Ad> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Ad::getAdId, adId).set(Ad::getStatus, false);
        return update(lambdaUpdateWrapper);
    }

    @Override
    public boolean onAd(Long adId) {
        LambdaUpdateWrapper<Ad> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Ad::getAdId, adId).set(Ad::getStatus, true);
        return update(lambdaUpdateWrapper);
    }

    @Override
    public Page<Ad> query(Ad ad, Integer current, Integer size) {
        LambdaQueryWrapper<Ad> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ad.getTitle() != null, Ad::getTitle, ad.getTitle());
        return page(new Page<>(current, size), lambdaQueryWrapper);
    }
}
